package com.osdp.sc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.util.ConvertUtil;
import com.osdp.sc.entity.ScRoleUser;
import com.osdp.sc.mapper.ScRoleUserMapper;
import com.osdp.sc.service.IScRoleUserService;
import com.osdp.sc.uitl.Fields;
import com.osdp.sc.vo.ScRoleUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScRoleUserServiceImpl extends ServiceImpl<ScRoleUserMapper, ScRoleUser> implements IScRoleUserService {

    @Override
    @Transactional
    public boolean batachSaveOrUpdateUserRoleByUserId(String userId, List<String> roleIds){
        this.deleteByUserId(userId);//直接先删除，再增加
        if(!CollectionUtils.isEmpty(roleIds)){
            //List<UcUserRole>  userRoleList =ConvertUtil.sourceToTarget(userRoleVoList, UcUserRole.class);
            List<ScRoleUser> userRoleList = new LinkedList<>();
            for(String id : roleIds){
                ScRoleUser entity = new ScRoleUser();
                entity.setRoleId(id);
                entity.setUserId(userId);
                userRoleList.add(entity);
            }
            return super.saveBatch(userRoleList);
        }
        return true;
    }

    @Override
    public List<ScRoleUserVo> queryUserRoleByUserId(String userId){
        List<ScRoleUser>  userRoleList = super.list(new QueryWrapper<ScRoleUser>()
                .eq(StringUtils.isNotBlank(userId), Fields.ScRoleUserQ.userId, userId));
        return ConvertUtil.sourceToTarget(userRoleList, ScRoleUserVo.class);
    }

    @Override
    public List<String> queryRoleIdByUserId(String userId){
        List<ScRoleUserVo> list = queryUserRoleByUserId(userId);
        return list.stream().map(ScRoleUserVo::getRoleId).collect(Collectors.toList());
    }

    @Override
    public boolean deleteByUserId(String userId){
        if(StringUtils.isNotBlank(userId)) {
            return super.remove(new QueryWrapper<ScRoleUser>()
                    .eq(Fields.ScRoleUserQ.userId, userId));
        }
        return false;
    }

    @Override
    public boolean deleteByRoleId(String roleId){
        return super.remove(new QueryWrapper<ScRoleUser>()
                .eq(StringUtils.isNotBlank(roleId), Fields.ScRoleUserQ.roleId, roleId));
    }

    @Override
    public List<ScRoleUserVo> queryUserRoleByRoleId(String roleId) {
        List<ScRoleUser>  userRoleList = super.list(new QueryWrapper<ScRoleUser>()
                .eq(StringUtils.isNotBlank(roleId),Fields.ScRoleUserQ.roleId, roleId));
        return ConvertUtil.sourceToTarget(userRoleList, ScRoleUserVo.class);

    }

    /**
     * 根据角色ID和多个用户ID保存用户与角色关系
     * @param roleId
     * @param userIds
     * @return
     */
    @Override
    @Transactional
    public boolean batachSaveOrUpdateUserRoleByRoleId(String roleId, List<String> userIds) {
        this.deleteByRoleId(roleId);//直接先删除，再增加
        boolean isSuccess = true;
        if(!CollectionUtils.isEmpty(userIds)){
            List<ScRoleUser> userRoleList = new LinkedList<>();
            for(String id : userIds){
                ScRoleUser entity = new ScRoleUser();
                entity.setRoleId(roleId);
                entity.setUserId(id);
                userRoleList.add(entity);
            }
            isSuccess =  super.saveBatch(userRoleList);
        }
       return isSuccess;
    }
}
